package com.isoft.uaaservice.handler;



import com.isoft.uaaservice.bean.ResponseData;
import com.isoft.uaaservice.bean.ResultCode;
import com.isoft.uaaservice.util.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.error("AccessDeniedHandlerImpl:"+ accessDeniedException.getClass().getName() + "--" + accessDeniedException.getMessage()+"--访问被拒绝!");
        //返回客户端访问被拒绝的json代码即可
        ResponseData responseData = new ResponseData();
        responseData.setErrorCode(ResultCode.ACCESS_DENIED.getCode());
        responseData.setMsg(ResultCode.ACCESS_DENIED.getMessage());
        WebUtils.renderObj(response , responseData);
    }
}
